<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Auth;

class ArtController extends Controller
{
    //详情
    public function show(Request $request,$aid)
    {
        //分类
        $cats = DB::table('cat')->join('art', 'art.cat_id', '=', 'cat.cat_id')->select('cat.cat_id','cat.catname',DB::raw('count(art.cat_id) as num'))->groupBy('art.cat_id')->get();
        $catskey = array_column(\GuzzleHttp\json_decode($cats->toJson(),true),null,'cat_id');

        //文章
        $detail = DB::table('art')->select('art_id','title','content','pubtime','view','cat_id')->where('art_id',$aid)->first();
        $detail->catname = $catskey[$detail->cat_id]['catname'];

        $tdk = [];
        $tdk['title'] = $detail->title.'-侯体宗的博客';
        $tdk['keywords'] = $detail->title.'_侯体宗的博客';
        $tdk['description'] = mb_substr(strip_tags($detail->content),0,190);

        //进来浏览量加一
        DB::table('art')->where('art_id',$aid)->increment('view');

        //根据id 取评论
        $comments = DB::table('comment')->where('art_id',$aid)->get()->toJson();
        $comments = $this->arrtree(\GuzzleHttp\json_decode($comments,true),'comment_id','fcomment_id');

        //文章归档 格式：2017年01月
        //$sql = "select pubtime,FROM_UNIXTIME( `pubtime`,'%Y-%m') as time ,count(*) as num FROM art where  is_state=0 and is_del=1 group by time" ;
        $file = DB::table('art')->select('pubtime',DB::raw('FROM_UNIXTIME(pubtime,\'%Y-%m\') as time'),DB::raw('count(*) as num'))->where('cat_id',$detail->cat_id)->where('is_state',0)->where('is_del',1)->groupBy('time')->get();

        //标签
        //$tags = Db::name('tag')->group('tagname')->order('tag_id desc')->limit(50)->column('tagname','tag_id');
        $tags = DB::table('tag')->select('*')->groupBy('tagname')->orderBy('tag_id','desc')->take(50)->pluck('tagname','tag_id');

        return view('home.art.detail', ['tdk'=>$tdk,'comments'=>$comments,'tags'=>$tags,'file'=>$file,'detail'=>$detail,'cats'=>$cats]);
    }

    //对文章写评论
    public function comment()
    {
        $comment = request()->input();
        $data['art_id'] = $comment['art_id'];
        $data['comment'] = $comment['comment'];
        $data['fcomment_id'] = isset($comment['fcomment_id'])?$comment['fcomment_id']:0;

        $data['user_id'] = auth()->id();
        $data['username'] = auth()->user()->name;
        $data['email'] = auth()->user()->email;
        $data['face'] = auth()->user()->profile_photo_path;

        $data['pubtime'] = time();
        $data['ip'] = request()->ip();

        DB::table('comment')->insert($data);
        return redirect('/art/'.$data['art_id']);
    }

    //对评论点赞
    public function dianzan()
    {
        $data = request()->input();
        if ($data) {
            $id = $data['id'];
            $res = DB::table('comment')->where('comment_id',$id)->increment('zan');
            if ($res) {
                $zan = Db::table('comment')->where('comment_id',$id)->value('zan');//获取最新点赞数
                echo json_encode(array('error' => 1, 'info' => '点赞成功', 'zan' => $zan));
            }else {
                echo json_encode(array('error' => 0, 'info' => '点赞失败'));
            }
        }
    }

    /**
     * 无极限分类 ：一维数据数组生成数据树
     * @param array $list 数据列表
     * @param string $id 父ID Key
     * @param string $pid ID Key
     * @param string $son 定义子数据Key
     * @return Collection
     */
    public function arrtree($list, $id = 'id', $pid = 'pid', $son = 'sub')
    {
        list($tree, $map) = [[], []];
        foreach ($list as $item) {
            $map[$item[$id]] = $item;
        }

        foreach ($list as $item) {
            if (isset($item[$pid]) && isset($map[$item[$pid]])) {
                $map[$item[$pid]][$son][] = &$map[$item[$id]];
            } else {
                $tree[] = &$map[$item[$id]];
            }
        }
        unset($map);
        return $tree;
    }

}
